package com.lww.feature.test.generics;

import java.beans.IntrospectionException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.lww.feature.test.reflectionIntrospector.ExcelColumnName;

/**
 * User: yian
 * Date: 2017/5/7
 */
public class GenericeBean1 {

    public <T> void pritlnAll(List<T> lists,Class t) throws IntrospectionException, IllegalAccessException {
        List<Field> fields = Arrays.stream(t.getDeclaredFields())
                .filter(p-> Optional.ofNullable(p.getAnnotation(ExcelColumnName.class)).isPresent())
                .collect(Collectors.toList());//        List<String> titles = fields.stream().map(p -> p.getAnnotation(ExcelColumnName.class).value()).collect(Collectors.toList());
        List<String> titles = fields.stream().map(p -> p.getAnnotation(ExcelColumnName.class))
                .map(p->p.title()).collect(Collectors.toList());

        titles.stream().forEach(p-> System.out.println(p));

        lists.stream().forEach(p->
            {fields.stream().forEach(param -> {
                    try {
                        param.setAccessible(true);
                        Object o = Optional.ofNullable(param.get(p))
                                .orElse(param.getAnnotation(ExcelColumnName.class).defaultValue());
                        System.out.print(o.toString());
                        System.out.print("|");

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                });
                System.out.println("");
            });
    }
}
